package com.youdianstar.app.common.widget;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
 * File: CheckHelper
 * Author: 82149 Create: 2021/10/11 17:32
 * Changes (from 2021/10/11)
 * --------------------------------------------------
 * description: 勾选列表
 * ---------------------------------------------------
 */
public abstract class CheckHelper {
    public interface HandleStateChange {
        void stateChange(RecyclerView.ViewHolder holder, Boolean check);

        void clickWhich(int position);
    }

    public CheckHelper(HandleStateChange handleStateChange) {
        this.handleStateChange = handleStateChange;
    }

    abstract void bindViewHolder(RecyclerView.ViewHolder viewHolder, View clickView);

    abstract boolean isCheckedPosition(int position);

    /**
     * 如果点击事件不是整个item的话，传入要点击的view的id
     *
     * @param viewId 点击的View
     */
    public void bindViewHolder(RecyclerView.ViewHolder viewHolder, int viewId) {
        bindViewHolder(viewHolder, viewHolder.itemView.findViewById(viewId));
    }

    /**
     * 默认点击事件是整个item
     *
     * @param viewHolder holder
     */
    public void bindViewHolder(RecyclerView.ViewHolder viewHolder) {
        bindViewHolder(viewHolder, viewHolder.itemView);
    }


    /*用来处理选中状态改变后状况，比如你想修改文本内容*/
    public void stateChange(RecyclerView.ViewHolder viewHolder, Boolean checked) {
        viewHolder.itemView.setSelected(checked);
        handleChange(viewHolder, checked);
    }

    public HandleStateChange handleStateChange;

    protected void handleChange(RecyclerView.ViewHolder viewHolder, boolean checked) {
        if (handleStateChange != null) {
            handleStateChange.stateChange(viewHolder, checked);
        }
    }

    public void setHandleStateChange(HandleStateChange handleStateChange) {
        this.handleStateChange = handleStateChange;
    }

    protected void clickWhich(int position){
        if (handleStateChange != null) {
            handleStateChange.clickWhich(position);
        }
    }
}
